home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / pippki.jar / content / pippki / device_manager.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  13.7 KB  |  417 lines

  1.  /*
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is mozilla.org code.
  13.  *
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 2001 Netscape Communications Corporation. All
  17.  * Rights Reserved.
  18.  *
  19.  * Contributor(s):
  20.  *  Bob Lord <lord@netscape.com>
  21.  *  Ian McGreer <mcgreer@netscape.com>
  22.  */
  23.  
  24. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  25. const nsFilePicker = "@mozilla.org/filepicker;1";
  26. const nsIPKCS11Slot = Components.interfaces.nsIPKCS11Slot;
  27. const nsIPKCS11Module = Components.interfaces.nsIPKCS11Module;
  28. const nsPKCS11ModuleDB = "@mozilla.org/security/pkcs11moduledb;1";
  29. const nsIPKCS11ModuleDB = Components.interfaces.nsIPKCS11ModuleDB;
  30. const nsIPK11Token = Components.interfaces.nsIPK11Token;
  31. const nsPK11TokenDB = "@mozilla.org/security/pk11tokendb;1";
  32. const nsIPK11TokenDB = Components.interfaces.nsIPK11TokenDB;
  33.  
  34. var bundle;
  35. var secmoddb;
  36.  
  37. /* Do the initial load of all PKCS# modules and list them. */
  38. function LoadModules()
  39. {
  40.   bundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  41.   secmoddb = Components.classes[nsPKCS11ModuleDB].getService(nsIPKCS11ModuleDB);
  42.   var modules = secmoddb.listModules();
  43.   var done = false;
  44.   try {
  45.     modules.isDone();
  46.   } catch (e) { done = true; }
  47.   while (!done) {
  48.     var module = modules.currentItem().QueryInterface(nsIPKCS11Module);
  49.     if (module) {
  50.       var slotnames = [];
  51.       var slots = module.listSlots();
  52.       var slots_done = false;
  53.       try {
  54.         slots.isDone();
  55.       } catch (e) { slots_done = true; }
  56.       while (!slots_done) {
  57.         var slot = null;
  58.      try {
  59.           slot = slots.currentItem().QueryInterface(nsIPKCS11Slot);
  60.         } catch (e) { slot = null; }
  61.         // in the ongoing discussion of whether slot names or token names
  62.         // are to be shown, I've gone with token names because NSS will
  63.         // prefer lookup by token name.  However, the token may not be
  64.         // present, so maybe slot names should be listed, while token names
  65.         // are "remembered" for lookup?
  66.     if (slot != null) {
  67.           if (slot.tokenName)
  68.             slotnames[slotnames.length] = slot.tokenName;
  69.           else
  70.             slotnames[slotnames.length] = slot.name;
  71.     }
  72.         try {
  73.           slots.next();
  74.         } catch (e) { slots_done = true; }
  75.       }
  76.       AddModule(module.name, slotnames);
  77.     }
  78.     try {
  79.       modules.next();
  80.     } catch (e) { done = true; }
  81.   }
  82.   /* Set the text on the fips button */
  83.   SetFIPSButtonText();
  84. }
  85.  
  86. function SetFIPSButtonText()
  87. {
  88.   var fipsButton = document.getElementById("fipsbutton");
  89.   var label;
  90.   if (secmoddb.isFIPSEnabled) {
  91.    label = bundle.GetStringFromName("disable_fips"); 
  92.   } else {
  93.    label = bundle.GetStringFromName("enable_fips"); 
  94.   }
  95.   fipsButton.setAttribute("label", label);
  96. }
  97.  
  98. /* Add a module to the tree.  slots is the array of slots in the module,
  99.  * to be represented as children.
  100.  */
  101. function AddModule(module, slots)
  102. {
  103.   var tree = document.getElementById("device_list");
  104.   var item  = document.createElement("treeitem");
  105.   var row  = document.createElement("treerow");
  106.   var cell = document.createElement("treecell");
  107.   cell.setAttribute("label", module);
  108.   row.appendChild(cell);
  109.   item.appendChild(row);
  110.   var parent = document.createElement("treechildren");
  111.   for (var i = 0; i<slots.length; i++) {
  112.     var child_item = document.createElement("treeitem");
  113.     var child_row = document.createElement("treerow");
  114.     var child_cell = document.createElement("treecell");
  115.     child_cell.setAttribute("label", slots[i]);
  116.     child_row.appendChild(child_cell);
  117.     child_item.appendChild(child_row);
  118.     child_item.setAttribute("pk11kind", "slot");
  119.     parent.appendChild(child_item);
  120.   }
  121.   item.appendChild(parent);
  122.   item.setAttribute("pk11kind", "module");
  123.   item.setAttribute("open", "true");
  124.   item.setAttribute("container", "true");
  125.   tree.appendChild(item);
  126. }
  127.  
  128. var selected_slot;
  129. var selected_module;
  130.  
  131. /* get the slot selected by the user (can only be one-at-a-time) */
  132. function getSelectedItem()
  133. {
  134.   var tree = document.getElementById('device_tree');
  135.   if (tree.currentIndex < 0) return;
  136.   var item = tree.contentView.getItemAtIndex(tree.currentIndex);
  137.   selected_slot = null;
  138.   selected_module = null;
  139.   if (item) {
  140.     var kind = item.getAttribute("pk11kind");
  141.     var module_name;
  142.     if (kind == "slot") {
  143.       // get the module cell for this slot cell
  144.       var cell = item.parentNode.parentNode.firstChild.firstChild;
  145.       module_name = cell.getAttribute("label");
  146.       var module = secmoddb.findModuleByName(module_name);
  147.       // get the cell for the selected row (the slot to display)
  148.       cell = item.firstChild.firstChild;
  149.       var slot_name = cell.getAttribute("label");
  150.       selected_slot = module.findSlotByName(slot_name);
  151.     } else { // (kind == "module")
  152.       // get the cell for the selected row (the module to display)
  153.       cell = item.firstChild.firstChild;
  154.       module_name = cell.getAttribute("label");
  155.       selected_module = secmoddb.findModuleByName(module_name);
  156.     }
  157.   }
  158. }
  159.  
  160. function enableButtons()
  161. {
  162.   var login_toggle = "true";
  163.   var logout_toggle = "true";
  164.   var pw_toggle = "true";
  165.   var unload_toggle = "true";
  166.   getSelectedItem();
  167.   if (selected_module) {
  168.     unload_toggle = "false";
  169.     showModuleInfo();
  170.   } else if (selected_slot) {
  171.     // here's the workaround - login functions are all with token,
  172.     // so grab the token type
  173.     var selected_token = selected_slot.getToken();
  174.     if (selected_token != null) {
  175.       if (selected_token.needsLogin() || !(selected_token.needsUserInit)) {
  176.         pw_toggle = "false";
  177.         if(selected_token.needsLogin()) {
  178.           if (selected_token.isLoggedIn()) {
  179.             logout_toggle = "false";
  180.           } else {
  181.             login_toggle = "false";
  182.           }
  183.         }
  184.       }
  185.     }
  186.     showSlotInfo();
  187.   }
  188.   var thebutton = document.getElementById('login_button');
  189.   thebutton.setAttribute("disabled", login_toggle);
  190.   thebutton = document.getElementById('logout_button');
  191.   thebutton.setAttribute("disabled", logout_toggle);
  192.   thebutton = document.getElementById('change_pw_button');
  193.   thebutton.setAttribute("disabled", pw_toggle);
  194.   thebutton = document.getElementById('unload_button');
  195.   thebutton.setAttribute("disabled", unload_toggle);
  196.   // not implemented
  197.   //thebutton = document.getElementById('change_slotname_button');
  198.   //thebutton.setAttribute("disabled", toggle);
  199. }
  200.  
  201. // clear the display of information for the slot
  202. function ClearInfoList()
  203. {
  204.   var info_list = document.getElementById("info_list");
  205.   while (info_list.firstChild)
  206.       info_list.removeChild(info_list.firstChild);
  207. }
  208.  
  209. // show a list of info about a slot
  210. function showSlotInfo()
  211. {
  212.   ClearInfoList();
  213.   switch (selected_slot.status) {
  214.    case nsIPKCS11Slot.SLOT_DISABLED:
  215.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  216.                 bundle.GetStringFromName("devinfo_stat_disabled"), 
  217.                 "tok_status");
  218.      break;
  219.    case nsIPKCS11Slot.SLOT_NOT_PRESENT:
  220.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  221.                 bundle.GetStringFromName("devinfo_stat_notpresent"), 
  222.                 "tok_status");
  223.      break;
  224.    case nsIPKCS11Slot.SLOT_UNINITIALIZED:
  225.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  226.                 bundle.GetStringFromName("devinfo_stat_uninitialized"), 
  227.                 "tok_status");
  228.      break;
  229.    case nsIPKCS11Slot.SLOT_NOT_LOGGED_IN:
  230.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  231.                 bundle.GetStringFromName("devinfo_stat_notloggedin"), 
  232.                 "tok_status");
  233.      break;
  234.    case nsIPKCS11Slot.SLOT_LOGGED_IN:
  235.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  236.                 bundle.GetStringFromName("devinfo_stat_loggedin"), 
  237.                 "tok_status");
  238.      break;
  239.    case nsIPKCS11Slot.SLOT_READY:
  240.      AddInfoRow(bundle.GetStringFromName("devinfo_status"), 
  241.                 bundle.GetStringFromName("devinfo_stat_ready"), 
  242.                 "tok_status");
  243.      break;
  244.   }
  245.   AddInfoRow(bundle.GetStringFromName("devinfo_desc"), 
  246.              selected_slot.desc, "slot_desc");
  247.   AddInfoRow(bundle.GetStringFromName("devinfo_manID"), 
  248.              selected_slot.manID, "slot_manID");
  249.   AddInfoRow(bundle.GetStringFromName("devinfo_hwversion"),
  250.              selected_slot.HWVersion, "slot_hwv");
  251.   AddInfoRow(bundle.GetStringFromName("devinfo_fwversion"),
  252.              selected_slot.FWVersion, "slot_fwv");
  253. }
  254.  
  255. function showModuleInfo()
  256. {
  257.   ClearInfoList();
  258.   AddInfoRow(bundle.GetStringFromName("devinfo_modname"),
  259.              selected_module.name, "module_name");
  260.   AddInfoRow(bundle.GetStringFromName("devinfo_modpath"),
  261.              selected_module.libName, "module_path");
  262. }
  263.  
  264. // add a row to the info list, as [col1 col2] (ex.: ["status" "logged in"])
  265. function AddInfoRow(col1, col2, cell_id)
  266. {
  267.   var tree = document.getElementById("info_list");
  268.   var item  = document.createElement("treeitem");
  269.   var row  = document.createElement("treerow");
  270.   var cell1 = document.createElement("treecell");
  271.   cell1.setAttribute("label", col1);
  272.   cell1.setAttribute("crop", "never");
  273.   row.appendChild(cell1);
  274.   var cell2 = document.createElement("treecell");
  275.   cell2.setAttribute("label", col2);
  276.   cell2.setAttribute("crop", "never");
  277.   cell2.setAttribute("id", cell_id);
  278.   row.appendChild(cell2);
  279.   item.appendChild(row);
  280.   tree.appendChild(item);
  281. }
  282.  
  283. // log in to a slot
  284. function doLogin()
  285. {
  286.   getSelectedItem();
  287.   // here's the workaround - login functions are with token
  288.   var selected_token = selected_slot.getToken();
  289.   try {
  290.     selected_token.login(false);
  291.     var tok_status = document.getElementById("tok_status");
  292.     if (selected_token.isLoggedIn()) {
  293.       tok_status.setAttribute("label", 
  294.                           bundle.GetStringFromName("devinfo_stat_loggedin"));
  295.     } else {
  296.       tok_status.setAttribute("label",
  297.                        bundle.GetStringFromName("devinfo_stat_notloggedin"));
  298.     }
  299.   } catch (e) {
  300.     var alertStr = bundle.GetStringFromName("login_failed"); 
  301.     alert(alertStr);
  302.   }
  303.   enableButtons();
  304. }
  305.  
  306. // log out of a slot
  307. function doLogout()
  308. {
  309.   getSelectedItem();
  310.   // here's the workaround - login functions are with token
  311.   var selected_token = selected_slot.getToken();
  312.   try {
  313.     selected_token.logout(false);
  314.     var tok_status = document.getElementById("tok_status");
  315.     if (selected_token.isLoggedIn()) {
  316.       tok_status.setAttribute("label", 
  317.                           bundle.GetStringFromName("devinfo_stat_loggedin"));
  318.     } else {
  319.       tok_status.setAttribute("label",
  320.                        bundle.GetStringFromName("devinfo_stat_notloggedin"));
  321.     }
  322.   } catch (e) {
  323.   }
  324.   enableButtons();
  325. }
  326.  
  327. // load a new device
  328. function doLoad()
  329. {
  330.   //device.loaddlg.width=300
  331.   //device.loaddlg.height=200
  332.   var dlgWidth = bundle.GetStringFromName("device.loaddlg.width");
  333.   var dlgHeight = bundle.GetStringFromName("device.loaddlg.height");
  334.   //
  335.   window.open("load_device.xul", "loaddevice", 
  336.               "chrome,width=" + dlgWidth + ",height="+ dlgHeight+ ",resizable=1,dialog=1,modal=1");
  337.   var device_list = document.getElementById("device_list");
  338.   while (device_list.firstChild)
  339.     device_list.removeChild(device_list.firstChild);
  340.   LoadModules();
  341. }
  342.  
  343. function doUnload()
  344. {
  345.   getSelectedItem();
  346.   if (selected_module) {
  347.     pkcs11.deletemodule(selected_module.name);
  348.     var device_list = document.getElementById("device_list");
  349.     while (device_list.firstChild)
  350.       device_list.removeChild(device_list.firstChild);
  351.     LoadModules();
  352.   }
  353. }
  354.  
  355. function changePassword()
  356. {
  357.   getSelectedItem();
  358.   window.open("changepassword.xul",
  359.               selected_slot.tokenName, 
  360.               "chrome,resizable=1,modal=1,dialog=1");
  361.   showSlotInfo();
  362.   enableButtons();
  363. }
  364.  
  365. // browse fs for PKCS#11 device
  366. function doBrowseFiles()
  367. {
  368.   var srbundle = srGetStrBundle("chrome://pippki/locale/pippki.properties");
  369.   var fp = Components.classes[nsFilePicker].createInstance(nsIFilePicker);
  370.   fp.init(window,
  371.           srbundle.GetStringFromName("loadPK11TokenDialog"),
  372.           nsIFilePicker.modeOpen);
  373.   fp.appendFilters(nsIFilePicker.filterAll);
  374.   if (fp.show() == nsIFilePicker.returnOK) {
  375.     var pathbox = document.getElementById("device_path");
  376.     pathbox.setAttribute("value", fp.file.persistentDescriptor);
  377.   }
  378. }
  379.  
  380. function doLoadDevice()
  381. {
  382.   var name_box = document.getElementById("device_name");
  383.   var path_box = document.getElementById("device_path");
  384.   pkcs11.addmodule(name_box.value, path_box.value, 0,0);
  385.   window.close();
  386. }
  387.  
  388. // -------------------------------------   Old code
  389.  
  390. function showTokenInfo()
  391. {
  392.   ClearInfoList();
  393.   getSelectedToken();
  394.   AddInfoRow(bundle.GetStringFromName("devinfo_label"), 
  395.              selected_token.tokenLabel, "tok_label");
  396.   AddInfoRow(bundle.GetStringFromName("devinfo_manID"),
  397.              selected_token.tokenManID, "tok_manID");
  398.   AddInfoRow(bundle.GetStringFromName("devinfo_serialnum"), 
  399.              selected_token.tokenSerialNumber, "tok_sNum");
  400.   AddInfoRow(bundle.GetStringFromName("devinfo_hwversion"),
  401.              selected_token.tokenHWVersion, "tok_hwv");
  402.   AddInfoRow(bundle.GetStringFromName("devinfo_fwversion"),
  403.              selected_token.tokenFWVersion, "tok_fwv");
  404. }
  405.  
  406. function toggleFIPS()
  407. {
  408.   secmoddb.toggleFIPSMode();
  409.   //Remove the existing listed modules so that re-fresh doesn't 
  410.   //display the module that just changed.
  411.   var device_list = document.getElementById("device_list");
  412.   while (device_list.firstChild)
  413.     device_list.removeChild(device_list.firstChild);
  414.  
  415.   LoadModules();
  416. }
  417.